package ads_config

import (
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
	"server/apps/ads/ads_fld"
	"server/global"
	"server/model/common/request"
	"server/model/common/response"
	"server/utils"
)

type AdsConfigApi struct {
}
type AdsConfigData struct {
	adsConfig AdsConfig
	adsFld    ads_fld.AdsFld
}

var adsConfigService = AdsConfigService{}
var adsFldService = ads_fld.AdsFldService{}

func (adsConfigApi *AdsConfigApi) CreateAdsConfig(c *gin.Context) {
	var adsConfigData AdsConfigData
	err := c.ShouldBindJSON(&adsConfigData)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	adsConfigData.adsConfig.CreatedBy = utils.GetUserID(c)
	if err := adsConfigService.CreateAdsConfig(&adsConfigData.adsConfig); err != nil {
		global.Logger.Error("创建失败!", zap.Error(err))
		response.FailWithMessage("创建失败", c)
	} else {
		response.OkWithMessage("创建成功", c)
	}
	if err := adsFldService.CreateAdsFld(&adsConfigData.adsFld); err != nil {
		global.Logger.Error("创建失败!", zap.Error(err))
		response.FailWithMessage("创建失败", c)
	} else {
		response.OkWithMessage("创建成功", c)
	}
}

func (adsConfigApi *AdsConfigApi) DeleteAdsConfig(c *gin.Context) {
	var adsConfig AdsConfig
	err := c.ShouldBindJSON(&adsConfig)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	adsConfig.DeletedBy = utils.GetUserID(c)
	if err := adsConfigService.DeleteAdsConfig(adsConfig); err != nil {
		global.Logger.Error("删除失败!", zap.Error(err))
		response.FailWithMessage("删除失败", c)
	} else {
		response.OkWithMessage("删除成功", c)
	}
	if err := adsFldService.DeleteAdsFldByApiId(adsConfig.ID); err != nil {
		global.Logger.Error("删除失败!", zap.Error(err))
		response.FailWithMessage("删除失败", c)
	} else {
		response.OkWithMessage("删除成功", c)
	}

}

func (adsConfigApi *AdsConfigApi) DeleteAdsConfigByIds(c *gin.Context) {
	var IDS request.IdsReq
	err := c.ShouldBindJSON(&IDS)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	deletedBy := utils.GetUserID(c)
	if err := adsConfigService.DeleteAdsConfigByIds(IDS, deletedBy); err != nil {
		global.Logger.Error("批量删除失败!", zap.Error(err))
		response.FailWithMessage("批量删除失败", c)
	} else {
		response.OkWithMessage("批量删除成功", c)
	}
}

func (adsConfigApi *AdsConfigApi) UpdateAdsConfig(c *gin.Context) {
	var adsConfig AdsConfig
	err := c.ShouldBindJSON(&adsConfig)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	adsConfig.UpdatedBy = utils.GetUserID(c)
	if err := adsConfigService.UpdateAdsConfig(adsConfig); err != nil {
		global.Logger.Error("更新失败!", zap.Error(err))
		response.FailWithMessage("更新失败", c)
	} else {
		response.OkWithMessage("更新成功", c)
	}
}

func (adsConfigApi *AdsConfigApi) FindAdsConfig(c *gin.Context) {
	var adsConfig AdsConfig
	err := c.ShouldBindQuery(&adsConfig)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if readsConfig, err := adsConfigService.GetAdsConfigById(adsConfig.ID); err != nil {
		global.Logger.Error("查询失败!", zap.Error(err))
		response.FailWithMessage("查询失败", c)
	} else {
		response.OkWithData(gin.H{"readsConfig": readsConfig}, c)
	}
}

func (adsConfigApi *AdsConfigApi) GetAdsConfigList(c *gin.Context) {
	var pageInfo AdsConfigSearch
	err := c.ShouldBindQuery(&pageInfo)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if list, total, err := adsConfigService.GetAdsConfigInfoList(pageInfo); err != nil {
		global.Logger.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败", c)
	} else {
		response.OkWithDetailed(response.PageResult{
			List:     list,
			Total:    total,
			Page:     pageInfo.Page,
			PageSize: pageInfo.PageSize,
		}, "获取成功", c)
	}
}

func (adsConfigApi *AdsConfigApi) GetAllAdsConfigs(c *gin.Context) {
	adsConfig, err := adsConfigService.GetAllAdsConfigs()
	if err != nil {
		global.Logger.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败", c)
	} else {
		response.OkWithDetailed(gin.H{"adsConfig": adsConfig}, "获取成功", c)
	}
}

func (adsConfigApi *AdsConfigApi) GetAdsConfig(c *gin.Context) {
	adsConfig, err := adsConfigService.GetAdsConfig()
	if err != nil {
		global.Logger.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败", c)
	} else {
		response.OkWithDetailed(gin.H{"adsConfig": adsConfig}, "获取成功", c)
	}
}
